import java.io.File; 
import java.io.IOException; 
import java.io.PrintWriter; 
import java.util.Iterator; 
import java.util.List; 

import javax.print.attribute.standard.Destination;
import javax.servlet.ServletConfig; 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import org.apache.commons.fileupload.FileItem; 
import org.apache.commons.fileupload.FileUploadException; 
import org.apache.commons.fileupload.disk.DiskFileItemFactory; 
import org.apache.commons.fileupload.servlet.ServletFileUpload; 

public class createFolder extends HttpServlet 
{ 
/** 
* 
*/ 
private static final long serialVersionUID = 1L; 

private static final String TMP_DIR_PATH = "/temp"; 
private File tmpDir; 
private static final String DESTINATION_DIR_PATH ="/images/"; //Folder under your server where uploaded file will be saved. 
private File destinationDir; 

public void init(ServletConfig config) throws ServletException 
{ 
super.init(config); 
String realPath = getServletContext().getRealPath(""); 

//System.out.println("Real path where file will be saved : "+realPath); 

//tmpDir = new File(realPath+TMP_DIR_PATH); 
//if(!tmpDir.exists()) 
//tmpDir.mkdir(); 

destinationDir = new File(realPath+DESTINATION_DIR_PATH); 
if(!destinationDir.exists()) 
destinationDir.mkdir();
destinationDir = new File(realPath+DESTINATION_DIR_PATH+"/hosts"); 
if(!destinationDir.exists()) 
destinationDir.mkdir(); 
}
@SuppressWarnings("unchecked") 
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
PrintWriter out = response.getWriter(); 
response.setContentType("text/plain"); 

DiskFileItemFactory fileItemFactory = new DiskFileItemFactory (); 
/* 
*Set the size threshold, above which content will be stored on disk. 
*/ 
fileItemFactory.setSizeThreshold(2*1024*1024); //Maximum file size which can be uploaded to server = 2 MB 
/* 
* Set the temporary directory to store the uploaded files of size above threshold. 
*/ 
fileItemFactory.setRepository(tmpDir);
} 
} 
